package com.gp.geits.service.impl;

import com.gp.geits.entity.UserEntity;
import com.gp.geits.service.UserService;
import com.gp.geits.utils.JwtInfo;
import com.gp.geits.utils.PageUtils;
import com.gp.geits.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.gp.geits.dao.PositionDao;
import com.gp.geits.entity.PositionEntity;
import com.gp.geits.service.PositionService;

import javax.servlet.http.HttpServletRequest;


@Service("positionService")
public class PositionServiceImpl extends ServiceImpl<PositionDao, PositionEntity> implements PositionService {

    @Autowired
    private UserService userService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PositionEntity> page = this.page(
                new Query<PositionEntity>().getPage(params),
                new QueryWrapper<PositionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void positionPublish(PositionEntity positionEntity, HttpServletRequest request) {
        JwtInfo jwtInfo = userService.loginCheck(request);
        Long no = jwtInfo.getNo();

        Integer userId = userService.getOne(new QueryWrapper<UserEntity>()
                .eq("phone", no)
                .or()
                .eq("stu_no", no)).getId();

        positionEntity.setUserId(userId);
        positionEntity.setUserName(jwtInfo.getName());
        positionEntity.setPublishTime(new Date());

        baseMapper.insert(positionEntity);
    }

    @Override
    public List<PositionEntity> positionByUserId(HttpServletRequest request) {
        Long no = userService.loginCheck(request).getNo();

        Integer userId = userService.getOne(new QueryWrapper<UserEntity>()
                .eq("phone", no)
                .or()
                .eq("stu_no", no)).getId();

        return baseMapper.selectList(new QueryWrapper<PositionEntity>()
                .eq("user_id",userId));
    }

}